<?php
/**
 * Created by CuiLiwu
 * User: Administrator
 * Date: 2018-04-03
 * Time: 10:00
 */

namespace App\Http\Controllers\Web\Member\Seller;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class ContractsController extends BaseController
{
    protected $api_url = [
        // 请求列表
        'index' => ['get', 'seller/contracts'],
        // 查询单条
        'show' => ['get', 'seller/contracts/%d'],
        // 单条创建
        'store' => ['post', 'seller/contracts'],
        // 单条更新
        'update' => ['post', 'seller/contracts/%d'],
        // 单条删除
        'destroy' => ['delete', 'seller/contracts/%d'],
        // 设为默认
        'update_set_default' => ['put', 'seller/contracts/%d/set_default'],
        'company_info'=>['get','seller/companymaterial/%d/showusercompany'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 列表页
     * method get
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //参数部分
        $params = [
            'orderBy' => 'updated_at',
            'sortedBy' => 'desc',
            'page'=>$this->request->get('page',1)
        ];

        //请求部分
        $ret  = http_web($this->api_url['index'])->send($params);
//        dd($ret['data']);
        $data = paginate($this->request, $ret['data']);
        return view('web.user.seller.contract_list', $data);
    }

    /**
     * 详情页
     * method get
     * url /$id
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id)
    {
        // 无详情页
    }

    /**
     * 创建页
     * method get
     * url /create
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $data = [];
        $info = http_web($this->api_url['company_info'],[session('user_id')])->send();
        if(isset($info['data']['data']['company'])){
            $company = $info['data']['data']['company'];
            $data['company'] = $company;
        }
        
        return view('web.user.seller.contract_detail', ['request_url' => route('contracts.store')],$data);
    }

    /**
     * 创建-form提交
     * method post
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function store()
    {
        //验证部分
        $data = $this->form();

        //请求部分
        $res = http_web($this->api_url['store'])->send($data);
        //跳转至列表页，如果有详情页，跳转至详情页
        return redirect()->route('contracts.index');
    }

    /**
     * 更新页
     * method get
     * url /$id/edit
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        //请求详情
        $contracts = http_web($this->api_url['show'], [$id])->send();

        //form old塞值
        old_set($contracts['data']);
        
        $company = [];
        $info = http_web($this->api_url['company_info'],[session('user_id')])->send();
        if(isset($info['data']['data']['company'])){
            $company = $info['data']['data']['company'];
        }

        return view('web.user.seller.contract_detail', ['request_url' => route('contracts.update', $id), 'type' => 'put', 'contracts'=>$contracts['data'],'company'=>$company]);
    }

    /**
     * 更新-form提交
     * method put
     * url /$id
     * @param $id
     */
    public function update($id)
    {
        //验证部分
        $data = $this->form();

        //请求部分
        $ss = http_web($this->api_url['update'], [$id])->send($data);
        //结果处理部分
        return redirect()->route('contracts.index');
    }

    /**
     * 更新-设置默认值
     * method put
     * url /$id/set_default
     * @param $id
     */
    public function updateSetDefault($id)
    {
        //请求部分
        $ret = http_web($this->api_url['update_set_default'], [$id])->send();

        //结果处理部分
        return $ret;
    }

    /**
     * 删除-ajax提交
     * method delete
     * url /$id
     * @param $id
     */
    public function destroy($id)
    {
//        dd('destroy');
        //请求部分
        $ret = http_web($this->api_url['destroy'], [$id])->send();
        return $ret;
    }

    /**
     * 创建、更新公共验证部分
     * @return array
     */
    private function form()
    {
        $this->validate($this->request, [
            'name' => 'required',
            'content' => 'required',
        ], [
            'name.required' => '合同名称不可为空',
            'content.required' => '请填写合同条例',
        ]);

        return $this->request->all(['name', 'content']);
    }
}